import {
  // eslint-disable-next-line camelcase
  checkIDCard_province,
  // eslint-disable-next-line camelcase
  checkIDCard_birthday,
  checkParity
} from './IDCardVerification.js'
const api = {
  urlObjFunc(url, obj) {
    let data = ''
    let num = 0
    for (const i in obj) {
      data = `${data}${num > 0 ? '&' : ''}${i}=${obj[i]}`
      num++
    }
    url = `${url}?${data}`
    console.log('url', url)
    return url
  },
  formatMoney(value) {
    return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  },
  /**
   * 跳转
   * @param {Object} url
   * @param {Object} params
   */
  toUrl(url, params = null) {
    if (params) {
      const paramsArr = []
      Object.keys(params).forEach(key => {
        paramsArr.push(`${key}=${params[key]}`)
      })
      url += '?' + paramsArr.join('&')
    }

    wx.navigateTo({
      url
    })
  },
  isLogin() {
    if (!wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return false
    } else {
      return true
    }
  },
  // eslint-disable-next-line camelcase
  checkFormatIDCardNumber(idCardNumber) {
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (idCardNumber === '') {
      return false
      // eslint-disable-next-line brace-style
      // 校验长度，类型
    } else if (reg.test(idCardNumber) === false) {
      console.log('错误1')
      return false
    } else if (checkIDCard_province(idCardNumber) === false) {
      console.log('错误2')
      // 校验省份
      return false
    } else if (checkIDCard_birthday(idCardNumber) === false) {
      console.log('错误3', checkIDCard_birthday(idCardNumber))
      // 校验生日
      return false
    } else if (checkParity(idCardNumber) === false) {
      console.log('错误4')
      // 检验位的检测
      return false
    }
    return true
  },
  // 手机号验证
  validatePhone(str) {
    const reg = /^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$/
    return reg.test(str)
  },
  // 邮箱验证
  validateEmail(str) {
    const reg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    return reg.test(str)
  },
  // 将所有标签替换成空
  replaceTag(str) {
    const newStr = str.replace(/<[^<>]+>/g, '')
    return newStr
  },
  // 昵称是否只由中英文、数字、“-”组成
  validateNickname(str) {
    const reg = /^[a-zA-Z0-9\u4e00-\u9fa5-]+$/
    return reg.test(str)
  },
  // 倒计时  时分秒
  countDown(endTime) {
    const totalTime = new Date(endTime.replace(/-/g, '/')).getTime() - new Date().getTime()
    let countDown = ''
    if (totalTime > 0) {
      const h = Math.floor((totalTime % (3600 * 24 * 1000)) / 1000 / 3600).toString().padStart(2, '0')
      const minutes = Math.floor((((totalTime % (3600 * 24 * 1000)) / 1000) % 3600) / 60).toString().padStart(2, '0')
      const seconds = Math.floor((((totalTime % (3600 * 24 * 1000)) / 1000) % 3600) % 60).toString().padStart(2, '0')
      countDown = h + ':' + minutes + ':' + seconds
    }
    return countDown
  },
  // 求某个时间距离现在为 几天几小时
  leadTime(targetTimeStr) {
    const currentTime = new Date()
    const targetTime = new Date(targetTimeStr)

    // 计算时间差（以毫秒为单位）
    const timeDiff = targetTime.getTime() - currentTime.getTime()

    // 将时间差转换为小时、分钟和秒数
    const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24))
    const hours = Math.floor(timeDiff / (1000 * 60 * 60) % 24)
    // const minutes = Math.floor((timeDiff / (1000 * 60)) % 60)
    // const seconds = Math.floor((timeDiff / 1000) % 60)
    return days + '天' + hours + '小时'
  }
}

export default api
